package com.fdb.efp.nls.api.rest;

import com.fdb.basic.framework.core.constant.DataRuleType;
import com.fdb.basic.framework.core.constant.PubConstant;
import com.fdb.basic.framework.core.constant.ResponseData;
import com.fdb.basic.framework.web.controller.AbstractController;
import com.fdb.efp.e4a.service.facade.SRoleDataRuleService;
import com.fdb.efp.nls.api.service.RiskmUtilService;
import com.fdb.efp.nls.service.facade.ColltDelayCaseRecordService;
import com.fdb.efp.nls.service.vo.ColltDelayCaseRecordVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/api")
public class ColltDelayCaseRecordController extends AbstractController {

    @Autowired
    @Qualifier("colltDelayCaseRecordService")
    private ColltDelayCaseRecordService colltDelayCaseRecordService;

    @Autowired
    @Qualifier("sRoleDataRuleService")
    private SRoleDataRuleService sRoleDataRuleService;

    @Autowired
    private RiskmUtilService riskmUtilService;


    private static final String CONTENTTYPE = "application/json;charset=UTF-8";

    private static final String MODELNAME = "催收任务延案申请模板.xls";

    private static final String IMPORT_PATH = "/template/colltDelayCaseImport.xls";

    /**
     * 列表数据查询
     *
     * @param vo
     * @return
     */
    @RequestMapping(value = "/collt/delay/case/records", method = RequestMethod.POST)
    public @ResponseBody
    ResponseData<List<ColltDelayCaseRecordVO>> queryColltDelayCaseRecordAll(@RequestBody ColltDelayCaseRecordVO vo) {
        List<ColltDelayCaseRecordVO> outputVo = null;
        setUserInfoToVO(vo);
        // 根据用户角色获取用户权限方案
        Map<String, String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
        vo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null : dataRuleTypeMap.get(PubConstant.PRD_CODE));
        DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null : dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
        setUserInfoToVO(vo);
        switch (dataRuleType) {
            case Owner:
                outputVo = colltDelayCaseRecordService.queryAllOwner(vo);
                break;
            case CurrOrg:
                outputVo = colltDelayCaseRecordService.queryAllCurrOrg(vo);
                break;
            case CurrDownOrg:
                outputVo = colltDelayCaseRecordService.queryAllCurrDownOrg(vo);
                break;
            default:
                outputVo = colltDelayCaseRecordService.queryAllOwner(vo);
                break;
        }
        riskmUtilService.displayOrgName(outputVo,"opOrgCode","opOrgName");
        riskmUtilService.displayUserName(outputVo,"opUserCode","opUserName");
        return getResponseData(outputVo);
    }

    /**
     * 根据主键查询详情
     *
     * @return
     */
    @RequestMapping(value = "/collt/delay/case/record", method = RequestMethod.GET)
    public @ResponseBody
    ResponseData<ColltDelayCaseRecordVO> queryByPk() {
        ColltDelayCaseRecordVO inVo = new ColltDelayCaseRecordVO();
        ColltDelayCaseRecordVO outputVo = colltDelayCaseRecordService.queryByPk(inVo);
        return getResponseData(outputVo);

    }

    /**
     * 根据主键删除信息
     *
     * @param inColltDelayCaseRecordVo
     * @return
     */
    @RequestMapping(value = "/collt/delay/case/record", method = RequestMethod.DELETE)
    public @ResponseBody
    ResponseData<Integer> deleteByPk(@RequestBody ColltDelayCaseRecordVO inColltDelayCaseRecordVo) {
        int outputVo = colltDelayCaseRecordService.deleteByPk(inColltDelayCaseRecordVo);
        return getResponseData(outputVo);

    }

    /**
     * 根据主键更新信息
     *
     * @param inColltDelayCaseRecordVo
     * @return
     */
    @RequestMapping(value = "/collt/delay/case/record", method = RequestMethod.PUT)
    public @ResponseBody
    ResponseData<Integer> updateByPk(@RequestBody ColltDelayCaseRecordVO inColltDelayCaseRecordVo) {
        int outputVo = colltDelayCaseRecordService.updateByPk(inColltDelayCaseRecordVo);
        return getResponseData(outputVo);

    }


    /**
     * 模板下载
     *
     * @param response
     * @param request
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/collt/delay/app/template/downLoad", method = RequestMethod.GET)
    public @ResponseBody
    ResponseData<String> colltDelayModelDownLoad(HttpServletResponse response, HttpServletRequest request) throws IOException {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            response.setContentType(CONTENTTYPE);
            /**
             * getBytes()，需要声明一下读取的编码方式，不然IE浏览器，会出现文件名乱码，谷歌不会
             * 【解决方法】 getBytes("GBK")
             */
            response.setHeader("Content-Disposition", "attachment;filename=" + new String((MODELNAME).getBytes("GBK"), "ISO8859-1"));
            ServletOutputStream out = response.getOutputStream();
            logger.info("下载催收任务延案申请导入模板，开始----------------------------");
            bis = new BufferedInputStream(ColltDelayCaseRecordController.class.getClassLoader().getResourceAsStream(IMPORT_PATH));
            bos = new BufferedOutputStream(out);
            byte[] buff = new byte[2048];
            int bytesRead;
            while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
                bos.write(buff, 0, bytesRead);
            }
        } catch (Exception e) {
            logger.error("模板文件下载失败：", e);
            return getResponseData(null);
        } finally {
            if (null != bis) {
                bis.close();
            }
            if (null != bos) {
                bos.close();
            }
        }
        logger.info("下载催收任务延案申请导入模板，结束。。。。。。。。。。。。。。。。。。。");
        return getResponseData("success!");
    }


}
